package com.ngmaster.apiserver.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "API接口相应对象")
public class BaseApiResponse {

    @ApiModelProperty(value = "响应码", required = true, example = "200")
    public Integer code;

    @ApiModelProperty(value = "响应消息", required = true, example = "200")
    private String msg;

    public static BaseApiResponse of() {
        return new BaseApiResponse();
    }

    public <T extends BaseApiResponse> T withResultSuccess() {
        return withResultStatus(ResponseStatus.SUCCESS);
    }

    public <T extends BaseApiResponse> T withResultStatus(ResponseStatus status) {
        return withResultCode(status.getCode()).withResultMsg(status.getMsg());
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseApiResponse> T withResultCode(Integer resultCode) {
        setCode(resultCode);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseApiResponse> T withResultMsg(String resultMsg) {
        setMsg(resultMsg);
        return (T) this;
    }

    public void success() {
        this.setCode(ResponseStatus.SUCCESS.getCode());
        this.setMsg("操作成功");
    }

}
